var tigerStep1 = {
	load  : function () {
		this.setFromValue();
		this.eventRegister();
	},
	
	setFromValue : function () {
		if (ad != null) {
		   if (getUser().Account  != ad.account) {
		   		Ext.getDom("adUserName").innerHTML  = '設計' + getUser().Name + '的廣告'; 
		   }
		   
		   if (ad.title) {
				Ext.getDom("title").value = ad.title;
		   }
		   if (ad.image1) {
				Ext.getDom("picImgage1").src = ad.image1;
				Ext.getDom("image1").value = ad.image1;
		   }
		   
		   if (ad.image2) {
		   		Ext.getDom("picImgage2").src = ad.image2;
				Ext.getDom("image2").value = ad.image2;
		   }
		   if (ad.url) {
		   		Ext.getDom("url").value = ad.url;
		   }	
				
		   if (ad.clickAction) {
		   		Ext.getDom("clickAction").value = ad.clickAction;
		   		tigerStep1.changeActionSample();
		   }	
		}
	},
	
	eventRegister : function () {
		Ext.get("btnImage1").on("click", this.uploadImg1);
		Ext.get("btnImage2").on("click", this.uploadImg2);
		Ext.get("btnNext").on("click", this.next);
		Ext.get("clickAction").on("change", this.changeActionSample);
	},
	
	changeActionSample : function () {
		
		 var dom = Ext.getDom("actionSample");
		
	     switch(Ext.getDom("clickAction").value){
	     	case '0' : dom.innerHTML = "(範例 http://mmedia.ai-ad.net.tw)"; break;
	     	case '1' : dom.innerHTML = "(範例 http://mmedia.ai-ad.net.tw)"; break;
	     	case '2' : dom.innerHTML = "(範例 tel:0922111222)"; break;
	     	case '3' : dom.innerHTML = "(範例 sms:22223312)"; break;
	     	case '4' : dom.innerHTML = "(範例 geo:0,0?q=台北市重慶南路一段57號)"; break;
	     	default : dom.innerHTML = ""; 
	     }
	}, 
	
	 
		
	
	uploadImg1 : function () {
		
		var result = null;
		    
		if (getBrowser().isIE) {
		 	result = uploadImg('ad', 30 , 390);
		} else if (getBrowser().isFF) {
			result = uploadImg('ad', 115 , 525);
		} else {
		 	result = uploadImg('ad', 110 , 520);
		}
		  
		 
		 if (result != null) {
		 	Ext.getDom("picImgage1").src = result;
		 	Ext.getDom("image1").value = result;
		 }
	},
	
	uploadImg2 : function () {
		 var result = null;
		    
		if (getBrowser().isIE) {
		 	result = uploadImg('ad', 30 , 390);
		} else if (getBrowser().isFF) {
			result = uploadImg('ad', 115 , 525);
		} else {
		 	result = uploadImg('ad', 110 , 520);
		}
		 if (result != null) {
		 	Ext.getDom("picImgage2").src = result;
		 	Ext.getDom("image2").value = result;
		 }
	},
	
	
	next : function () {
		if (!Ext.getDom("title").value) {
			alert('請輸入[標題]!');
			Ext.getDom("title").focus();
			return;
		}
		
		if (!Ext.getDom("image1").value && !Ext.getDom("image2").value) {
			alert("請上傳一組圖片!");
			return;
		}
		
		
		if (Ext.getDom("url").value == "") {
			alert("請輸入[目的頁面URL]");
			Ext.getDom("url").focus();
			return;
		} 
		
		/*
		if (!Ext.getDom("url").value.isUrl()){
			alert("請輸入正確URL!");
			Ext.getDom("url").focus();
			return;
		}*/
		 
		
		if (Ext.getDom("clickAction").value == "") {
			alert("請選擇[點擊廣告後動作]!");
			Ext.getDom("clickAction").focus();
			return;
		}
		
		Ext.getDom("form").submit();
	}
};

window.onload = function () {
	tigerStep1.load();	
}